package com.wanghengzhi.design.patterns.chain;

class ChainMain {

    /**
     * 责任链模式： 为请求创建一个接收者对象链。将请求者和接受者进行解耦，这些对象形成一个链，并沿着这个链传递请求，使得每个接受对象都有处理的机会
     * 知道处理完成。
     */
    public static void main(String[] args) {
        AbstractLogger loggerChain = getChainOfLoggers();

        loggerChain.logMsg(AbstractLogger.INFO, "This is an information.");

        loggerChain.logMsg(AbstractLogger.DEBUG,
                "This is a debug level information.");

        loggerChain.logMsg(AbstractLogger.ERROR,
                "This is an error information.");
    }

    private static AbstractLogger getChainOfLoggers(){

        AbstractLogger errorLogger = new ErrorLogger(AbstractLogger.ERROR);
        AbstractLogger fileLogger = new FileLogger(AbstractLogger.DEBUG);
        AbstractLogger consoleLogger = new ConsoleLogger(AbstractLogger.INFO);

        errorLogger.setNextLogger(fileLogger);
        fileLogger.setNextLogger(consoleLogger);

        return errorLogger;
    }

}
